package mdl

import (
	"hotime"
	"strings"
	//"fmt"
)

type Yuche struct {
	data []hotime.Map
}

func (this *Yuche) ReplacePan(needRep string) string {
	temp := strings.Replace(needRep, " ", "", -1)
	temp = strings.Replace(temp, "升", "", -1)
	temp = strings.Replace(temp, "降", "", -1)

	//            temp=temp.replace("受","");
	return temp
}

func (this *Yuche) ReplacePing(needRep string) float64 {
	temp := strings.Replace(needRep, " ", "", -1)
	temp = strings.Replace(temp, "↓", "", -1)
	temp = strings.Replace(temp, "↑", "", -1)
	num, _ := hotime.ObjToFloat64(temp)

	return num
}
func (this *Yuche) ShuiPing(temp float64) int {

	if temp >= float64(1) {
		return 2 //高水
	}
	if temp >= float64(0.9) { //中水
		return 1
	}
	return 0 //低水
}
func (this *Yuche) Huansuan(huansuan float64) int {
	//            console.log(huansuan)
	data := 4
	if huansuan > float64(0) {
		data = 3
	} else if huansuan == float64(0) {
		data = 5
	}

	return data
}
func (this *Yuche) SetAnswer(i int, key string) {

	var k = strings.Replace(this.data[i].GetString("tcpan"), "受", "", -1)
	//            console.log(k)
	if Pan[k] == nil {
		return
	}

	if Pan.GetMapPointer(k).GetMap(key)["c0"] != nil {
		this.data[i]["a0"] = Answer[Pan.GetMapPointer(k).GetMapPointer(key).GetString("c0")]
	}
	if Pan.GetMapPointer(k).GetMap(key)["c1"] != nil {
		this.data[i]["a1"] = Answer[Pan.GetMapPointer(k).GetMapPointer(key).GetString("c1")]
	}
	if Pan.GetMapPointer(k).GetMap(key)["c2"] != nil {
		this.data[i]["a2"] = Answer[Pan.GetMapPointer(k).GetMapPointer(key).GetString("c2")]
	}
}

func (this *Yuche) Jielun(i int, key string) {
	var len = len(Key)
	//            console.log(key+":"+key[1])
	//fmt.Println(key)
	if hotime.Substr(key, 3, 1) == "5" {
		key = strings.Replace(this.data[i].GetString("tcpan"), "受", "", -1) + "盘不变" + "," + hotime.Substr(key, 0, 3)
	} else {
		key = strings.Replace(this.data[i].GetString("tcpan"), "受", "", -1) + "盘" + strings.Replace(hotime.Substr(key, 3, strings.Count(key, "")-1),":","",-1) + "," + hotime.Substr(key, 0, 3)
	}

	//            console.log(key.substr(3,key.length))


	for j := 0; j < len; j++ {
		//key=key.replace(j,Key[j]);
		key=strings.Replace(key, hotime.ObjToStr(j), Key.GetString(j), -1)
		//fmt.Println(hotime.ObjToStr(j)+":"+ Key.GetString(j)+"=="+key)
	}

	//            console.log(Info.data[i])

	this.data[i]["key"] = key
}

func (this *Yuche) List(data []hotime.Map)[]hotime.Map {
	//return data
	this.data=data
	for i := 0; i < len(this.data); i++ {
		this.data[i]["tpan"] = this.ReplacePan(this.data[i].GetString("pan"))
		this.data[i]["tcpan"] = this.ReplacePan(this.data[i].GetString("cpan"))
		//初始盘口是否为受
		if strings.Compare(this.data[i].GetString("cpan"), "受") !=0{
			this.data[i]["tping"] = this.ReplacePing(this.data[i].GetString("ping"))
			this.data[i]["tcping"] = this.ReplacePing(this.data[i].GetString("cping"))
		} else {
			this.data[i]["tping"] = this.ReplacePing(this.data[i].GetString("ying"))
			this.data[i]["tcping"] = this.ReplacePing(this.data[i].GetString("cying"))
		}
		this.data[i]["shui"] = this.ShuiPing(this.data[i].GetFloat64("tping"))
		this.data[i]["cshui"] = this.ShuiPing(this.data[i].GetFloat64("tcping"))

		var huansuanPan = this.Huansuan(HuanSuan.GetFloat64(this.data[i].GetString("tpan")) - HuanSuan.GetFloat64(this.data[i].GetString("tcpan"))) //盘口升降
		var huansuanPing = this.Huansuan(this.data[i].GetFloat64("tping") - this.data[i].GetFloat64("tcping"))                               //水位升降

		//fmt.Println(this.data[i].GetFloat64("tping"))
		//fmt.Println(this.data[i].GetFloat64("tcping"))
		var key = this.data[i].GetString("tcpan")

		var temp = "" + this.data[i].GetString("cshui") + "" + hotime.ObjToStr(huansuanPing) + "" + this.data[i].GetString("shui")
		var temp1 string
		//是否去除临时盘受
		//if strings.Compare(key,"受") {
		temp1 = temp + hotime.ObjToStr(huansuanPan) + ":" + strings.Replace(this.data[i].GetString("tpan"), "受", "", -1)
		//}else{
		//	temp1 = temp + huansuanPan + ":" + this.data[i]["tpan"];
		//}

		//查找pan库并获取Answer结果
		if Pan[strings.Replace(key, "受", "", -1)] != nil && Pan.GetMap(strings.Replace(key, "受", "", -1))[temp1] != nil {

			this.SetAnswer(i, temp1) //二级答案

		}
		//拼接字符串
		this.Jielun(i, temp1)
		if this.data[i]["a0"] == nil {
			this.data[i]["a0"] = ""
		}
		if this.data[i]["a1"] == nil {
			this.data[i]["a1"] = ""
		}
		if this.data[i]["a2"] == nil {
			this.data[i]["a2"] = ""
		}
		if this.data[i]["key"] == nil {
			this.data[i]["key"] = nil
		}
	}
	return this.data
}


func (this *Yuche) Get(data hotime.Map)hotime.Map {
	dd:=make([]hotime.Map,1)
	dd[0]=data
	dd=this.List(dd)
	return dd[0]
}